<%@page import="org.stwy.pro.asset.AssetEntity"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<style type="text/css">
	.modal-title{
	font-weight: bold;
	}
</style>
<div class="modal fade" id="asset_change_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
	  <div class="modal-dialog">
	    <div class="modal-content">
	      <div class="modal-header">
	        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
	        <h4 class="modal-title" id="exampleModalLabel">资产状态变更</h4>
	      </div>
	      <div class="modal-body">
	        <form id="asset-form-change" >
	        <input type="hidden" name="stateAfter">
	        <input type="hidden" name="stateBefore">
	          <div class="form-group">
	          	<label for="recordDate" class="required">变更日期</label>
	            <input name="recordDate" type="text" class="rap-input-date">
	          </div>
	          <div class="form-group hide" id="div_canNotRentType">
	          	<label class="required" for="canNotRentType">不可出租类型</label>
	          	<select class="rap-input-enumdata" name="canNotRentType"
							data-enum-group="stwy_canNotRentType"></select>
	          </div>
	          <div class="form-group">
	          	<label for="reason" class="required">变更原因</label>
	            <textarea class="form-control" id="reason" name="reason"></textarea>
	          </div>
	        </form>
	      </div>
	      <div class="modal-footer">
	        <button type="button" class="btn btn-primary" id="sureModal">确定</button>
	        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
	      </div>
	    </div>
	  </div>
	</div>

<script type="text/javascript">
var state_null = "<%=AssetEntity.ASSET_STATUS_NULL%>";
var state_rent = "<%=AssetEntity.ASSET_STATUS_RENT%>";
var state_self = "<%=AssetEntity.ASSET_STATUS_SELF%>";
var state_cannotrent = "<%=AssetEntity.ASSET_STATUS_CANNOTRENT%>";

function changeAssetState(fromState,toState,title){
	var pks = RapDataTableUtils.getSelectPks();
	if (pks==null||pks.length < 1){
		  alert("请选择需要变更的资产！");
		  return false;
	}
	title=title==null?"资产状态变更":title;
	var result = checkCanChange(fromState,toState);
	if(result){
		var url = "${ctx}/pro/asset/addAssetRecord";
		setModalShow(fromState,toState);//设置隐藏域内容和界面显示
		popChange(title,url,pks,true,onRefresh);
	}
}
/**
 * @param title:弹出框的标题
 * @param url:请求服务的url
 * @param isShowSuccess:是否alert请求成功后返回的信息（data.msg）。
 * @param fnCallback:请求成功后的回调函数名，会把服务器返回的data作为参数进行回调
 */
function popChange(title,url,pks,isShowSuccess,fnCallback){
	  if(isShowSuccess==null){
		  isShowSuccess=true;
	  }
	  var modal = $("#asset_change_modal");
	  modal.find('.modal-title').text(title);
	  modal.find('#sureModal').unbind("click");
	  modal.find('#sureModal').bind("click",function(){
		  saveChange(url,pks,isShowSuccess,fnCallback);
	  });
  	  $('#asset_change_modal').modal({
		toggle:"modal"
	 });
}
function saveChange(url,pks,isShowSuccess,fnCallback){
	  var checkResult = checkInput();
	  if(!checkResult){
		  return false;
	  }
  	  var param = $("#asset_change_modal form").serializeArray();
  	  var pkArr = {name:"pks",value:pks.toString()};
  	  param.push(pkArr);
	  $.ajax({
		"dataType" : "json",
		"type" : "POST",
		"url" : url,
		"data" : param,
		"success" : function(data) {
			$('#asset_change_modal').modal('hide');
			if (data.success) {
				if(isShowSuccess){
					if(data.msg!=null&&data.msg.length>0)
						alert(data.msg);
					else
						alert("操作成功");
				}
				if (typeof (fnCallback) != "undefined")
					fnCallback(data);
			} else {
				alert(data.msg);
			}
		},
		"error" : function(XMLHttpRequest, textStatus, errorThrown) {
			alert("操作失败，HTTP错误。");
		}
	});
}
function checkInput(){
	$("span.error").remove();
	var recordDate = $("#asset_change_modal form input[name='recordDate']").val();
	var reason =  $("#asset_change_modal form textarea[name='reason']").val();
	var flag = true;
	if(recordDate==null||recordDate.length==0){
		$("#asset_change_modal form input[name='recordDate']").after("<span class='error'>请输入变更日期</span>");
		flag=false;
	}
	if(reason==null||reason.length==0){
		$("#asset_change_modal form textarea[name='reason']").after("<span class='error'>请输入变更原因</span>");
		flag=false;
	}
	if(!$("#div_canNotRentType").hasClass("hide")){
		var canNotRentType = $("#asset_change_modal form select[name='canNotRentType']").val();
		if(canNotRentType==null||canNotRentType.length==0){
			$("#asset_change_modal form select[name='canNotRentType']").after("<span class='error'>请输入不可出租类型</span>");
			flag=false;
		}
	}
	return flag;
}
function checkCanChange(fromState,toState){
	if(fromState==state_null ){
		
	}
	if(fromState==state_rent ){
			
	}
	if(fromState==state_self ){
		
	}
	if(fromState==state_cannotrent ){
		
	}
	
	return true;
}
function setModalShow(fromState,toState){
	$("#asset_change_modal form")[0].reset();
	$("#asset_change_modal form input[name='stateBefore']").val(fromState);
	$("#asset_change_modal form input[name='stateAfter']").val(toState);
	if(state_cannotrent==toState){
		$("#div_canNotRentType").removeClass("hide");
	}else{
		$("#div_canNotRentType").addClass("hide");
	}
}
</script>
